package com.caltco.cargo.ops.auth.config;

import com.caltco.cargo.ops.auth.component.JwtAuthenticationTokenFilter;
import com.caltco.cargo.ops.auth.component.RestAuthenticationEntryPoint;
import com.caltco.cargo.ops.auth.component.RestfulAccessDeniedHandler;
import com.caltco.cargo.ops.common.autoconfigure.AgileFastConfiguration;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @program: agilefast-framework
 * @description: SpringSecurity配置类
 * @author: yehao
 * @create: 2022-07-20 10:32
 **/
@Configuration
public class SecurityConfig {

    @Resource
    private AgileFastConfiguration whiteListConfiguration;

    @Resource
    private RestfulAccessDeniedHandler restfulAccessDeniedHandler;
    @Resource
    private RestAuthenticationEntryPoint restAuthenticationEntryPoint;
    @Resource
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

//    @Autowired(required = false)
//    private DynamicSecurityService dynamicSecurityService;
//    @Autowired(required = false)
//    private DynamicSecurityFilter dynamicSecurityFilter;
    @Value("${agilefast.authentication.open}")
    private String authentication;

    @Bean
    SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry = http
                .authorizeRequests();
        //不需要保护的资源路径允许访问
        for (String url : whiteListConfiguration.getWhitelist()) {
            registry.requestMatchers(url).permitAll();
        }
        if(authentication.equals("false")){
            registry.requestMatchers(HttpMethod.POST).permitAll();
            registry.requestMatchers(HttpMethod.GET).permitAll();
        }
        //允许跨域请求的OPTIONS请求
        registry.requestMatchers(HttpMethod.OPTIONS)
                .permitAll();
        // 任何请求需要身份认证
        registry.and()
                .authorizeRequests()
                .anyRequest()
                .authenticated()
                // 关闭跨站请求防护及不使用session
                .and()
                .csrf()
                .disable()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                // 自定义权限拒绝处理类
                .and()
                .exceptionHandling()
                .accessDeniedHandler(restfulAccessDeniedHandler)
                .authenticationEntryPoint(restAuthenticationEntryPoint)
                // 自定义权限拦截器JWT过滤器
                .and()
                .addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
        //有动态权限配置时添加动态权限校验过滤器
//        if(dynamicSecurityService!=null){
//            registry.and().addFilterBefore(dynamicSecurityFilter, FilterSecurityInterceptor.class);
//        }
        return http.build();
    }


}
